package com.netease.nim.avchatkit.ui;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.netease.nim.avchatkit.R;

public class SendGiftView extends LinearLayout {
    private View root;
    private TextView tv_name;
    private TextView tv_gift_name;
    private ImageView ivGift;
    private TextView tv_num;

    private long showTime; //显示的时间
    private static final long INTERVAL_TIME = 3000;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if ((System.currentTimeMillis() - showTime) < INTERVAL_TIME) { //说明还没消失，再次发礼物了
                return;
            }
            root.setVisibility(View.GONE);
        }
    };

    public SendGiftView(Context context) {
        super(context);
        init(context);
    }

    public SendGiftView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SendGiftView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_send_gift, this);
        root = view.findViewById(R.id.root);
        tv_name = view.findViewById(R.id.tv_name);
        tv_gift_name = view.findViewById(R.id.tv_gift_name);
        ivGift = view.findViewById(R.id.iv_gift);
        tv_num = view.findViewById(R.id.tv_num);
    }

    public void setData(String name, String giftName, int srcResId, int num) {
        tv_name.setText(name);
        tv_gift_name.setText(giftName);
        ivGift.setImageResource(getResid(srcResId));
        tv_num.setText("" + num);
        root.setVisibility(View.VISIBLE);
        showTime = System.currentTimeMillis();
        mHandler.sendEmptyMessageDelayed(1, INTERVAL_TIME);
    }

    public void onDestroy() {
        mHandler.removeMessages(1);
    }

    private int getResid(int id) {
        int resId = R.mipmap.gift001;
        switch (id) {
            case 1:
                resId = R.mipmap.gift001;
                break;
            case 2:
                resId = R.mipmap.gift002;
                break;
            case 3:
                resId = R.mipmap.gift003;
                break;
            case 4:
                resId = R.mipmap.gift004;
                break;
            case 5:
                resId = R.mipmap.gift005;
                break;
            case 6:
                resId = R.mipmap.gift006;
                break;
            case 7:
                resId = R.mipmap.gift007;
                break;
            case 8:
                resId = R.mipmap.gift008;
                break;
        }
        return resId;
    }

}
